<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    // "浏览器代号: " + navigator.appCodeName + ""; 
    document.write("浏览器代号: " + navigator.appCodeName + "<br/>") // Mozilla fire Fox 
    //"浏览器名称: " + navigator.appName + "";
    document.write("浏览器名称: " + navigator.appName + "<br/>") //Netscape 网景
    // "浏览器版本: " + navigator.appVersion + "";
    document.write("浏览器版本: " + navigator.appVersion + "<br/>") //


    // "硬件平台: " + navigator.platform + "";
    document.write("浏览器版本: " + navigator.platform + "<br/>") //

    // ****重点
    // "用户代理: " + navigator.userAgent + "";  请求头 浏览器信息
    document.write("浏览器版本: " + navigator.userAgent + "<br/>") //


    // 判断浏览器函数
    function myBrowser() {
      var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串

      var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
      var isIE = userAgent.indexOf("compatible") > -1
        && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
      var isEdge = userAgent.indexOf("Edge") > -1; //判断是否IE的Edge浏览器

      var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器

      var isSafari = userAgent.indexOf("Safari") > -1
        && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器

      var isChrome = userAgent.indexOf("Chrome") > -1
        && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器
      // 判断Ie版本
      if (isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if (fIEVersion == 7) {
          return "IE7";
        } else if (fIEVersion == 8) {
          return "IE8";
        } else if (fIEVersion == 9) {
          return "IE9";
        } else if (fIEVersion == 10) {
          return "IE10";
        } else if (fIEVersion == 11) {
          return "IE11";
        } else {
          return "0";
        }//IE版本过低
        return "IE";
      }
      if (isOpera) {
        return "Opera";
      }
      if (isEdge) {
        return "Edge";
      }
      if (isFF) {
        return "FF";
      }
      if (isSafari) {
        return "Safari";
      }
      if (isChrome) {
        return "Chrome";
      }

    }
    // 谷歌浏览器
    if (myBrowser() == "Chrome") {
      document.body.style.backgroundColor = "skyblue"
    } else if (myBrowser() == "FF") {
      document.body.style.backgroundColor = "red"
    }

    if (navigator.userAgent.indexOf("Chrome") > -1 && navigator.userAgent.indexOf("Safari") > -1) {
      alert("当前在谷歌浏览器")
    } else if (navigator.userAgent.indexOf("Firefox") > -1) {
      alert("你当前在火狐浏览器")
    }
  </script>
</body>

</html>